import axios from 'axios'
import { Toast } from 'vant';
import {API_CONST} from '@/const/api'

let baseURL = process.env.BASE_URL

const axiosInstance = axios.create({
  baseURL: baseURL,
  withCredentials: true,
  timeout: 25000
})

axiosInstance.interceptors.request.use(function (config) {
  return config
}, function (error) {
  return Promise.reject(error)
})

axiosInstance.interceptors.response.use(function (response) {
    if (Number(response.data['code']) === API_CONST.UNAUTH) {
      // 如何未登录就把token删掉
      localStorage.removeItem('token')
      return Promise.reject(new Error(response.data.msg))
    }else if (Number(response.data['code']) !== API_CONST.SUCCESS) {
      Toast(response.data.msg);
      return Promise.reject(new Error(response.data.msg))
    } else {
      return response.data.data
      // throw new Error('网络错误')
    }
}, function () {
  // Message.error('网络超时')
  return false
})

export default axiosInstance
